<?php
declare (strict_types = 1);

namespace app\admin\controller\finance;

use app\common\model\AdminRole;
use app\common\model\SchoolExpenditure;
use app\common\model\SchoolPettyCash;
use think\facade\Db;
use think\facade\Request;
use app\common\service\FinanceAssets as S;
use app\common\model\FinanceAssets as M;
use app\common\model\SchoolPettyCash as P;

class Assets extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck'];

    // 列表
    public function index(){return $this->getAuto($this->fetch(),M::getList());}

    // 添加
    public function add(){
        if(Request::isPost()){
            $admin = session('admin');
            $data = Request::post();
            $data['admin_id'] = $admin['id'];
            $data['school_id'] = $admin['school_id'];
            $data['add_time'] = toDate(time());


            $role_id = Db::table('bl_admin_admin_role')
                ->where('admin_id',$admin['id'])
                ->column('role_id');
            //4财务端 5招生主管 6教学主任 7招生人员 9教师
//        if (in_array(7,$role_id)||in_array(9,$role_id)){
//            $data['director_audit'] = 1;
//        }
            if (in_array(5,$role_id)||in_array(6,$role_id)){
                $data['director_audit'] = 2;
            }
            if (in_array(4,$role_id)){
                $data['director_audit'] = 2;
                $data['finance_audit'] = 2;
            }
            P::create($data);
            return ['msg'=>'成功','code'=>200];
        }else{
            return $this->fetch('add',['project'=>$this->projectList()]);
        }
    }
    //消费项目
    public function projectList(){
        return Db::table('bl_school_consumptio_items')->select();
    }

    // 编辑
    public function edit($id){return $this->getAuto($this->fetch('',['model' => M::find($id)]),S::goEdit(Request::post(),$id));}

    // 状态
    public function status($id){return $this->getJson(S::goStatus(Request::post('status'),$id));}

    // 删除
    public function remove($id){return $this->getJson(S::goRemove($id));}

    // 批量删除
    public function batchRemove(){return $this->getJson(S::goBatchRemove(Request::post('ids')));}

    // 回收站
    public function recycle(){return $this->getAuto($this->fetch(),S::goRecycle());}


    public function petty_cash()
    {
        $limit = input('get.limit');
//        $admin = session('admin');
//        $where[] = ['principal_audit', '=', 2];
        $admin = session('admin');
        $where = [];
        if ($admin['school_id'] > 0) {
            $where[] = ['school_id', '=', $admin['school_id']];
        }
        $status = true;
        $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc')?:['role_id'=>1];
        // $users = AdminRole::where('admin_id', $admin['id'])->find();
        if ($users['role_id'] > 2) {
            //教师||招生人员
            if ($users['role_id']==9||$users['role_id']==7){
                $status = false;
                $where[] = ['admin_id','=',$admin['id']];
            }elseif ($users['role_id']==5||$users['role_id']==6){//招生主管||教学主管
                $admin_id = self::getSubordinateId($admin['id']);
                $where[] = ['admin_id','in',$admin_id];
                //主管待审核
                $where[] = ['director_audit','=',1];
            }elseif ($users['role_id']==4){//财务
                //主管审核通过
                $where[] = ['director_audit','=',2];
                $where[] = ['finance_audit','=',1];
            }elseif ($users['role_id']==8){//投资人
                //主管审核通过
                $where[] = ['director_audit','=',2];
                //财务审核通过
                $where[] = ['finance_audit','=',2];
            }else{//不显示
                $where[] = ['admin_id', '=', 0];
            }

//            $where[] = ['admin_id', '=', $admin['id']];
        }else{
            $status = false;
        }
        $list = SchoolPettyCash::with([
            'get_admin' => function($a) {
                $a->field('id,username');
            },'get_school' => function($a) {
                $a->field('id,school_name');
            }
        ])->order('id','desc')->where($where)->paginate($limit);
        $items = $list->items();
        foreach ($items as $k => $v) {
            $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc');
            if ($users) {
                $items[$k]['group'] = AdminRole::where('id', $users['role_id'])->value('name');
            } else {
                $items[$k]['group'] = '未分组';
            }
        }
        $data = ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
        return $this->getAuto($this->fetch('',['status'=>$status]), $data);
    }

    public function auditCash($id){
        $admin = session('admin');
        $role_id = Db::table('bl_admin_admin_role')
            ->where('admin_id',$admin['id'])
            ->column('role_id');
        $status = Request::param('finance_audit');
        if (in_array(5,$role_id)||in_array(6,$role_id)){//主管
            $data['director_audit'] = $status;
        }elseif (in_array(4,$role_id)){//财务
            $data['finance_audit'] = $status;
        }else{
            $data['investor_audit'] = $status;
        }
        $res = Db::table('bl_school_petty_cash')->where('id',$id)->update($data);
        if ($res){
            return ['code'=>200,'msg'=>'审核成功'];
        }else{
            return ['code'=>201,'msg'=>'失败'];
        }
    }
    public function investorCash($id){
        return $this->getJson(S::investorCash(Request::param('finance_audit'),$id));
    }
    public function receiveCash($id){return $this->getJson(S::receiveCash(Request::param('is_receive'),$id));}

    public function expenditure()
    {
        return $this->getAuto($this->fetch(),M::getExpenditureList());
    }
    //获取当前角色下的用户id
    public static function getSubordinateId($admin_id){
        return  Db::table('bl_admin_admin')
            ->where(['top_id'=>$admin_id])
            ->column('id');
    }

}
